﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing.Imaging;


namespace Expressing_Label
{
    public partial class InfoAnalysisFrm : Form
    {
        private bool c_isFrmMoving = false;
        private Point c_ptCurrentPosition = new Point();
        private List<string> xData = new List<string>();
        private List<int> yData = new List<int>();
        private List<double> dbYData = new List<double>();
        private int iTimerCount = 0;
        private const int CHANGETIMES = 40;

        public InfoAnalysisFrm()
        {
            InitializeComponent();
            InitializeData();
        }

        private void InitializeData()
        {
            //PublicResources pr = new PublicResources();
            //DataBind to Distribution Chart. XData:Expressing;  YData:final Sum
            
            foreach (var key in PublicResources.g_ExpressingSelectedIndex.Keys)
            {
                string s = key.ToString();
                xData.Add(s);
                yData.Add(PublicResources.g_ExpresingCaptureRecord[12, PublicResources.g_ExpressingSelectedIndex[s] + 5]);
            }
            chtDistribution.Series[0].Points.DataBindXY(xData, yData);
            

            //DataBind to SumByTime. XData:Time every 4 minutes;  YData:Sum in different 4 minutes
            xData.Clear();
            yData.Clear();
            int maxNum = 0;
            for (int i = 0; i < 10; i++)
            {
                xData.Add(((i + 1) * 3).ToString() + "分钟");
                yData.Add(0);
                if (PublicResources.g_ExpresingCaptureRecord[i, 0] > maxNum)
                    maxNum = PublicResources.g_ExpresingCaptureRecord[i, 0];
            }
            chtSumByTime.Series[0].Points.DataBindXY(xData, yData);
            chtSumByTime.ChartAreas[0].AxisY.Maximum = maxNum + 5;

            maxNum = 0;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 10; j++)
                    if (PublicResources.g_ExpresingCaptureRecord[j, i] > maxNum)
                        maxNum = PublicResources.g_ExpresingCaptureRecord[j, i];
                Series series = new Series();
                series.Legend = "Legend1";
                series.LegendText = PublicResources.g_ExpressingSourceItem[i * 5];
                series.ChartType = SeriesChartType.Spline;
                series.IsValueShownAsLabel = false;
                series.Points.DataBindXY(xData, yData);
                chtGroupSum.Series.Add(series);
            }
            chtGroupSum.ChartAreas[0].AxisY.Maximum = maxNum;
        }

        private void InfoAnalysisFrm_Load(object sender, EventArgs e)
        {
            lblSum.Text = "您共截图" + PublicResources.g_CaptureTimes.ToString() + "次.";
            lblAbandon.Text = "放弃" + (PublicResources.g_CaptureTimes - PublicResources.g_ExpresingCaptureRecord[12, 0]).ToString() + "次";
            lblUnAccepted.Text = "连续截图" + PublicResources.g_CaptureInTenSec.ToString() + "次";
            lblSuccess.Text = "有效截图" + PublicResources.g_ExpresingCaptureRecord[12,0].ToString() + "次";
            timDynamic.Start();
        }

        private void chtSum_MouseDown(object sender, MouseEventArgs e)
        {

            HitTestResult result = chtDistribution.HitTest(e.X, e.Y);
            
            if (result.ChartElementType == ChartElementType.DataPoint || result.ChartElementType == ChartElementType.DataPointLabel)
            {
                DataPoint selectedDataPoint = (DataPoint)result.Object;
                selectedDataPoint.CustomProperties = "Exploded = true";
            }
            
        }


        private void chtSum_MouseMove(object sender, MouseEventArgs e)
        {
            DataPoint tmp = new DataPoint();
            DataPoint selectedDataPoint = new DataPoint();
            Point point = chtDistribution.PointToClient(MousePosition);
            HitTestResult result = chtDistribution.HitTest(point.X, point.Y);
            if (result.ChartElementType == ChartElementType.DataPoint)
            {
                selectedDataPoint = (DataPoint)result.Object;
                if (tmp != selectedDataPoint)
                {
                    selectedDataPoint.ToolTip = "#VALX" + ": " + "#VAL";
                    tmp = selectedDataPoint;
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            SaveDataToFile();
            this.Owner.Show();
            this.Close();
        }

        private void SaveDataToFile()
        {
            if (!Directory.Exists(@"CaptureAnalysis"))
                Directory.CreateDirectory(@"CaptureAnalysis");
            string imgPath = @"CaptureAnalysis\" + PublicResources.g_iUserID.ToString();
            if (File.Exists(@imgPath + ".data"))
                File.Delete(@imgPath + ".data");
            if (File.Exists(@imgPath + ".png"))
                File.Delete(@imgPath + ".png");
            Bitmap b = new Bitmap(Width,Height);
            Graphics gra = Graphics.FromImage(b);
            gra.CopyFromScreen(this.Location, new Point(0, 0), this.Size);
            b.Save(@imgPath + ".png", ImageFormat.Png);
            string filePath = imgPath + ".data";
            FileStream fs = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            for (int i = 0; i < 15; i++)
            {
                string s = "";
                for (int j = 0; j < 25; j++)
                {
                    s += PublicResources.g_ExpresingCaptureRecord[i, j].ToString() + " ";
                }
                sw.WriteLine(s);
            }

            sw.Close();
            fs.Close();
            gra.Dispose();
            b.Dispose();
        }

        private void chtSumByTime_MouseDown(object sender, MouseEventArgs e)
        {
            c_isFrmMoving = true;
            c_ptCurrentPosition = MousePosition;
        }

        private void chtSumByTime_MouseMove(object sender, MouseEventArgs e)
        {
            if (c_isFrmMoving)
            {
                this.Left += MousePosition.X - c_ptCurrentPosition.X;
                this.Top += MousePosition.Y - c_ptCurrentPosition.Y;
                c_ptCurrentPosition = MousePosition;
            }
        }

        private void chtSumByTime_MouseUp(object sender, MouseEventArgs e)
        {
            c_isFrmMoving = false;
        }

        private void chtSumByTime_MouseLeave(object sender, EventArgs e)
        {
            c_isFrmMoving = false;
        }

        private void timDynamic_Tick(object sender, EventArgs e)
        {
            if (iTimerCount / 4 < chtDistribution.Series[0].Points.Count)
                chtDistribution.Series[0].Points[iTimerCount / 4].CustomProperties = "Exploded = true";
            iTimerCount++;
            if (iTimerCount > CHANGETIMES)
            {
                chtSumByTime.Series[0].IsValueShownAsLabel = true;
                for (int i = 0; i < 4; i++)
                {
                    chtGroupSum.Series[i].IsValueShownAsLabel = true;
                }
                timDynamic.Stop();
                return;
            }

            dbYData.Clear();
            for (int i = 0; i < 10; i++)
            {
                dbYData.Add(PublicResources.g_ExpresingCaptureRecord[i,0] *1.0 * iTimerCount /CHANGETIMES);
            }
            chtSumByTime.Series[0].Points.DataBindY(dbYData);

            
            for (int i = 1; i <= 4; i++)
            {
                dbYData.Clear();
                for (int j = 0; j < 10; j++)
                {
                    dbYData.Add(PublicResources.g_ExpresingCaptureRecord[j, i] * 1.0 * iTimerCount / CHANGETIMES);
                }
                chtGroupSum.Series[i - 1].Points.DataBindY(dbYData);
            }
        }

        private void InfoAnalysisFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                SaveDataToFile();
                this.Owner.Show();
                this.Close();
            }
        }
    }
}